package com.example.demo.com.example.proxy.cglibproxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibMeipo implements net.sf.cglib.proxy.MethodInterceptor {

    public Object getInstance(Class<?> clazz) throws Exception{
        //相当于Proxy，代理的工具类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //业务的增强
        before();
        Object obj = methodProxy.invokeSuper(o,objects); after();
        return obj;
    }

    private void before(){ System.out.println("我是媒婆:我要给你找对象，现在已经拿到你的需求"); System.out.println("开始物色");
    }
    private void after(){
        System.out.println("如果合适的话，就准备办事"); }
}
